<?php

namespace app\admin\model\shop\order;

use app\admin\model\shop\user\User;
use think\Model;
use think\model\concern\SoftDelete;
use think\model\relation\BelongsTo;
use think\model\relation\HasMany;
use think\model\relation\HasOne;

/**
 * Order
 */
class Order extends Model
{
    use SoftDelete;

    protected $defaultSoftDelete = 0;

    // 表名
    protected $name = 'shop_order';

    protected $type = [
        'user_contact'    => 'json',
        'discount_data'   => 'json',
        'discount_detail' => 'json',
        'create_time'     => 'timestamp: Y-m-d H:i:s',
        'update_time'     => 'timestamp: Y-m-d H:i:s',
        'pay_time'        => 'timestamp: Y-m-d H:i:s',
        'delivery_time'   => 'timestamp: Y-m-d H:i:s',
        'receive_time'    => 'timestamp: Y-m-d H:i:s',
        'evaluate_time'   => 'timestamp: Y-m-d H:i:s',
        'cancel_time'     => 'timestamp: Y-m-d H:i:s',
    ];

    protected $jsonAssoc = true;

    //关联详情
    public function detail(): HasMany
    {
        return $this->hasMany(OrderDetail::class, 'order_id', 'id');
    }

    //关联用户
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    //关联地址
    public function address(): HasOne
    {
        return $this->hasOne(OrderAddress::class, 'order_id', 'id');
    }

    public function statusLog(): HasMany
    {
        return $this->hasMany(OrderStatusLog::class, 'order_id', 'id');
    }

    public function orderRefund(): HasOne
    {
        return $this->hasOne(OrderRefund::class, 'order_id', 'id');
    }

    public function delivery(): HasMany
    {
        return $this->hasMany(OrderDelivery::class, 'order_id', 'id');
    }
}